##### This file produces Figures 1 & 2 from the coded file, Figure_1_2.csv.####


library(reshape)
library(ggplot2)
library(car)
library(foreign)
library(xtable)
library(stargazer)
library(plyr)
library(extrafont)


data1 <-read.csv("Figure_1_2.csv", header=TRUE, sep=",")


#### Produce Figure 1

data1$count <- 1

data1$sexT <- data1$web_sex
data1$laborT <- data1$web_labor

data1$sexT[data1$sexPlus==1] <- 1
data1$laborT[data1$sexPlus==1] <- 0

data1$sexT[data1$laborPlus==1] <- 0
data1$laborT[data1$laborPlus==1] <- 1

data1$sexF<- data1$sexT
data1$laborF <- data1$laborT


data1$sum <- data1$sexF + data1$laborF


data1$agg <- 999
data1$agg[data1$sexF==1 & data1$laborF==0] <- 1
data1$agg[data1$laborF==1 & data1$sexF==0] <- 2
data1$agg[data1$sum==2] <- 3

summary(as.factor(data1$agg))
full <- data1[which(data1$agg<4),]

attach(full)
full.o <- full[order(agg, date),]
detach(full)


full.t <- ddply(full.o, .(agg), transform, cdate=cumsum(count))

ggplot(full.t, aes(x=date, y=cdate, colour=as.factor(agg), group=as.factor(agg), linetype=as.factor(agg))) +
  geom_line() +
  ylab("Cummulative Number of Organizations\n") +
  xlab("\nYear") +
  scale_linetype_manual(name="Organization Focus", breaks=c('1',"2","3"),
                        values = c(1,2,3),
                          labels=c("Sex trafficking","Labor trafficking", "Both"))+ 
    scale_color_manual(name="Organization Focus", breaks=c('1',"2","3"),
                       values = c("Grey60", "grey40", "black"),
                       labels=c("Sex trafficking","Labor trafficking", "Both"))+
  theme_bw() + theme(text=element_text(family="Garamond"))

ggsave(file="BonillaMo_Figure1.jpg", width = 200, height = 100, units = "mm", dpi=600)


##### Produce Figure 2 
data1$ImmT <- 0
data1$ImmT[data1$web_immigrants==1|data1$immigrant_pop==1]<-1



data1$aggI <- 999
data1$aggI[data1$ImmT==1] <- 1
data1$aggI[data1$ImmT==0] <- 2


fullI <- data1[which(data1$aggI<999),]
attach(fullI)
fullI.o <- fullI[order(aggI, date),]
detach(fullI)


fullI.t <- ddply(fullI.o, .(aggI), transform, cdate=cumsum(count))


#### Producing Figure
ggplot(fullI.t, aes(x=date, y=cdate, colour=as.factor(aggI), group=as.factor(aggI), linetype=as.factor(aggI))) +
  geom_line() +
  ylab("Cummulative Number of Organizations\n") +
  xlab("\nYear") +
  scale_color_manual(name="Populations Served", breaks=c('1','2'),
                     values = c("grey60", "black"),
                     labels=c("Includes Foreign \nNationals", "Domestic Only")) +
  scale_linetype_manual(name="Populations Served", breaks=c('1',"2"),
                        values = c(1,2),
                        labels=c("Includes Foreign \nNationals", "Domestic Only"))+ 
  theme_bw() + theme(text=element_text(family="Garamond"))

ggsave(file="BonillaMo_Figure2.jpg", width = 200, height = 100, units = "mm", dpi=600)



####### Calculating the Number of Organizations In each Year #######
attach(full)
table(date, agg)
detach(full)



tab <- table(full$date,full$agg)

graph <- as.data.frame.matrix(tab)
graph$dates<- as.numeric(row.names(graph))

mean(graph$`1`[graph$dates>1971])
mean(graph$`1`[graph$dates>2000])
mean(graph$`2`[graph$dates>2003])
mean(graph$`2`[graph$dates<1997])
mean(graph$`2`[graph$dates>1971])
mean(graph$`3`[graph$dates>1971])
mean(graph$`3`[graph$dates<2001])


tab2 <- table(fullI$date,fullI$ImmT)
graph <- as.data.frame.matrix(tab2)
graph$dates<- as.numeric(row.names(graph))

mean(graph$`1`[graph$dates>2000])
mean(graph$`1`[graph$dates<2000])
mean(graph$`0`[graph$dates>2000])
mean(graph$`0`[graph$dates<2000])



